package controller

import (
	"admin/contrib/session"
	"admin/model"
	"common/helper"
	"encoding/json"
	"github.com/valyala/fasthttp"
)

type GradientController struct{}

func (that GradientController) GradientList(ctx *fasthttp.RequestCtx) {
	param := model.TblGradientParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	s, err := model.GetTblGradient(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that GradientController) GradientUpdate(ctx *fasthttp.RequestCtx) {
	param := model.TblGradient{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	admin := session.AdminGet(ctx)
	if admin["loginuser"] == "merchant" {
		s := model.GameConfigByType(279)
		if s.CfgValue == "0" {
			helper.RetData(ctx, false, "总台设置禁止编辑，保存失败")
			return
		}

	}
	if admin["loginuser"] == "business" {
		s := model.GameConfigByType(280)
		if s.CfgValue == "0" {
			helper.RetData(ctx, false, "总台设置禁止编辑，保存失败")
			return
		}

	}
	model.InsertLog("隐藏订单更新", ctx)
	err = model.UpdateOrDelTblGradient(param, 0, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that GradientController) GradientDelete(ctx *fasthttp.RequestCtx) {
	param := model.TblGradient{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("隐藏订单删除", ctx)
	err = model.UpdateOrDelTblGradient(param, 1, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}
